using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data.OleDb;

namespace Cache
{
    public partial class frmMain : Form
    {
        private BindingSource bindingSource1 = new BindingSource();
        private SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter();



        public frmMain()
        {
            InitializeComponent();
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Open the Open File Dialog and allow the user to select the file that
            //is to be opened, then process immediately.

            frmImport frmImport = new frmImport();
            frmImport.Show();

            /*
            OpenFileDialog OFD1 = new OpenFileDialog();
            OFD1.Title = "Select GPX File...";
            OFD1.InitialDirectory = Application.StartupPath.ToString();
            OFD1.Filter = "GPX Files (*.gpx)|*.gpx";
            OFD1.RestoreDirectory = true;
            
            if (OFD1.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;
                GPXReader gpx = new GPXReader();
                gpx.OpenGPXFile(OFD1.FileName.ToString());
                Cursor.Current = Cursors.Arrow;
            }
            */           
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = bindingSource1;
            GetData("select * from tblGeoCache");
        }

       
        private void tsFilterFound_Click(object sender, EventArgs e)
        {
            if (tsFilterFound.Checked == true)
            {
                //If the button is down then unselect the button and requery.
                tsFilterFound.Checked = false;
                dataGridView1.DataSource = bindingSource1;
                GetData("SELECT * FROM tblGeoCache");
            }
            else
            {
                tsFilterFound.Checked = true;
                dataGridView1.DataSource = bindingSource1;
                GetData("SELECT * FROM tblGeoCache WHERE Found = 'False'");
            }
            
        }

        private void GetData(string selectCommand)
        {
            try
            {
                String connString = "data source=" + Application.StartupPath + @"\GeoCaches.db";

                SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(selectCommand, connString);

                SQLiteCommandBuilder cmdBuilder = new SQLiteCommandBuilder(dataAdapter);

                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;

                //dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnMode.AllCellsExceptHeader);


            }
            catch (SQLiteException ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

    }
}